// BlogCollectionDao.java
package net.web.dao;

import net.web.common.utils.JdbcUtils;
import net.web.entity.BlogCollection;
import net.web.entity.Blog;

import java.util.List;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class BlogCollectionDao {
    public boolean insert(BlogCollection collection) {
        String sql = "INSERT INTO blog_collection (blog_id, user_id) VALUES (?,?)";
        int rows = JdbcUtils.executeUpdate(sql, collection.getBlogId(), collection.getUserId());
        return rows > 0;
    }

    public boolean delete(int blogId, int userId) {
        String sql = "DELETE FROM blog_collection WHERE blog_id = ? AND user_id = ?";
        return JdbcUtils.executeUpdate(sql, blogId, userId) > 0;
    }

    public boolean isCollected(int blogId, int userId) {
        String sql = "SELECT COUNT(*) FROM blog_collection WHERE blog_id = ? AND user_id = ?";
        long count = JdbcUtils.executeQueryOneValue(sql, blogId, userId);
        return count > 0;
    }

    public int getCollectionCount(int blogId) {
        String sql = "SELECT COUNT(*) FROM blog_collection WHERE blog_id = ?";
        return (int) JdbcUtils.executeQueryOneValue(sql, blogId);
    }

    public List<Blog> getCollectionsByUserId(int userId) {
        List<Blog> blogs = new ArrayList<>();
        String sql = "SELECT b.* FROM blog_collection bc JOIN blog b ON bc.blogid = b.id WHERE bc.userid = ?";
        try (Connection conn = JdbcUtils.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setInt(1, userId);
            ResultSet rs = pstmt.executeQuery();
            while (rs.next()) {
                Blog blog = new Blog();
                blog.setId(rs.getInt("id"));
                blog.setTitle(rs.getString("title"));
                blog.setContent(rs.getString("content"));
                blog.setExcerpt(rs.getString("excerpt"));
                blog.setCoverimage(rs.getString("coverimage"));
                blog.setClassid(rs.getInt("classid"));
                blog.setAuthorid(rs.getInt("authorid"));
                blog.setIsfeatured(rs.getInt("isfeatured"));
                blog.setIscommentable(rs.getInt("iscommentable"));
                blog.setViewcount(rs.getInt("viewcount"));
                blog.setLikecount(rs.getInt("likecount"));
                blog.setCollectioncount(rs.getInt("collectioncount"));
                blog.setMetatitle(rs.getString("metatitle"));
                blog.setMetadescription(rs.getString("metadescription"));
                blog.setMetakeywords(rs.getString("metakeywords"));
                blog.setPublishedat(rs.getString("publishedat"));
                blog.setCreatedat(rs.getString("createdat"));
                blog.setUpdatedat(rs.getString("updatedat"));
                blogs.add(blog);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return blogs;
    }
}
